package com.ruoyi.energy.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.energy.domain.NyTransAllow;
import com.ruoyi.energy.domain.vo.NyTransAllowDetailVO;
import com.ruoyi.energy.service.INyTransAllowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 箱变容量申请Controller
 *
 * @author cjie
 * @date 2022-03-05
 */
@Api(tags = " 箱变容量申请")
@RestController
@RequestMapping("/nyTransAllow")
public class NyTransAllowController extends BaseController {
    @Autowired
    private INyTransAllowService nyTransAllowService;

    /**
     * 查询箱变容量申请列表
     */
    @ApiOperation(value="查询箱变容量详情申请列表", notes="查询箱变容量详情申请列表")
    @PreAuthorize(hasPermi = "energy:nyTransAllow:list")
    @GetMapping("/list")
    public TableDataInfo list(NyTransAllow nyTransAllow) {
        startPage();
        List<NyTransAllowDetailVO> list = nyTransAllowService.selectNyTransAllowDetailVOList(nyTransAllow);
        return getDataTable(list);
    }

    /**
     * 根据transId查询 空开申请列表，返回Map<enName,List<></>></>
     */
    @ApiOperation(value="小程序查询transId空开列表", notes="微信查询transId空开列表")
    @PreAuthorize(hasPermi = "energy:nyTransAllow:list")
    @GetMapping("wx/list")
    public AjaxResult selectAllowDetailByTransId(String transId) {
        return AjaxResult.success(nyTransAllowService.selectNyTransAllowDetailVOListByTransId(transId));
    }

    /**
     * 查询箱变容量申请列表
     */
    @ApiOperation(value="查询箱变容量申请列表", notes="查询箱变容量申请列表")
    @PreAuthorize(hasPermi = "energy:nyTransAllow:list")
    @GetMapping("/listEn")
    public TableDataInfo listEn(NyTransAllow nyTransAllow) {
        startPage();
        List<NyTransAllow> list = nyTransAllowService.selectNyTransAllowList(nyTransAllow);
        return getDataTable(list);
    }

    /**
     * 导出箱变容量申请列表
     */
    @ApiOperation(value="导出箱变容量申请列表", notes="导出箱变容量申请列表")
    @PreAuthorize(hasPermi = "energy:nyTransAllow:export")
    @Log(title = "箱变容量申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NyTransAllow nyTransAllow) throws IOException {
        List<NyTransAllowDetailVO> list = nyTransAllowService.selectNyTransAllowDetailVOList(nyTransAllow);
        ExcelUtil<NyTransAllowDetailVO> util = new ExcelUtil<NyTransAllowDetailVO>(NyTransAllowDetailVO.class);
        util.exportExcel(response, list, "箱变容量申请数据");
    }
    /**
     * 获取箱变容量申请详细信息
     */
    @ApiOperation(value="获取箱变容量申请详细信息", notes="获取箱变容量申请详细信息")
    @PreAuthorize(hasPermi = "energy:nyTransAllow:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(nyTransAllowService.selectNyTransAllowById(id));
    }

    /**
     * 新增箱变容量申请
     */
    @ApiOperation(value="新增箱变容量申请", notes="新增箱变容量申请")
    @PreAuthorize(hasPermi = "energy:nyTransAllow:add")
    @Log(title = "箱变容量申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NyTransAllow nyTransAllow) {
        return toAjax(nyTransAllowService.insertNyTransAllow(nyTransAllow));
    }

    /**
     * 修改箱变容量申请
     */
    @ApiOperation(value="修改箱变容量申请", notes="修改箱变容量申请")
    @PreAuthorize(hasPermi = "energy:nyTransAllow:edit")
    @Log(title = "箱变容量申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NyTransAllow nyTransAllow) {
        return toAjax(nyTransAllowService.updateNyTransAllow(nyTransAllow));
    }

    /**
     * 删除箱变容量申请
     */
    @ApiOperation(value="删除箱变容量申请", notes="删除箱变容量申请")
    @ApiImplicitParam(name = "ids", value = "箱变容量申请ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "energy:nyTransAllow:remove")
    @Log(title = "箱变容量申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(nyTransAllowService.deleteNyTransAllowByIds(ids));
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<NyTransAllowDetailVO> util = new ExcelUtil<NyTransAllowDetailVO>(NyTransAllowDetailVO.class);
        util.importTemplateExcel(response, "仪表数据");
    }

    /**
     * 导入抄表记录列表,
     */
    @PreAuthorize(hasPermi = "energy:nyTransAllow:edit")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, String meterType) throws Exception {
        ExcelUtil<NyTransAllowDetailVO> util = new ExcelUtil<NyTransAllowDetailVO>(NyTransAllowDetailVO.class);
        List<NyTransAllowDetailVO> buiRoomVOList = util.importExcel(file.getInputStream());
        //批量插入数据
        String message = nyTransAllowService.importData(buiRoomVOList,meterType);
        return AjaxResult.success(message);
    }
}
